import time
# 引入爬虫框架
import requests as r
# xpath
from lxml import etree
# 正则
import re
# json格式化str为dict
import json
# 用于下载文件
import wget


# 下载文件用
def str_to_dict(string):
    # 使用json模块的loads函数将字符串转换为字典
    result = json.loads(string)
    return result


# 科大学习通通过识别Header检测是否爬虫，为防止连接超时未关闭，配置close
header = {
    'Connection': 'close',
    'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}

#
response = r.get("https://mooc1.chaoxing.com/mooc-ans/course/239369164.html", headers=header)
response = response.text.replace('\xa0', ' ')
html = etree.HTML(response)
print(response)
fileList = html.xpath("//div[@id='chapterResTab']//tr[@id!='resTableHead']")
print(fileList)
resourceList = []
if len(fileList) > 0:
    for file in fileList:
        fileName = file.xpath(".//td[@class=\"pt10 g6 resources_td\"]//text()")
        fileId = file.xpath("./@data")
        fileRealIdRe = re.search(r'download/(.*?)\?', fileId[0])
        fileRealId = fileRealIdRe.group(1)
        header2 = {
            'Referer': 'https://mooc1.chaoxing.com/ananas/modules/pdf/index.html?v=2023-1007-1958',
            'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
        }

        url = f"https://mooc1.chaoxing.com/ananas/status/{fileRealId}"
        response = r.get(url, headers=header2)
        a = str_to_dict(response.text)
        fileType = "unknown"
        fileUrl = "unknown"
        if a.get("pdf") is None:
            fileUrl = a.get("http")
            fileType = "mp4"
        else:
            fileUrl = a.get("pdf")
            fileType = "pdf"
        resource = {
            "classId": "238206797",
            "fileName": fileName if len(fileName) == 1 else fileRealId + ".mp4",
            "fileId": fileRealId,
            "type": fileType,
            "url": fileUrl
        }

        print(resource)
        # wget.download(a["pdf"], f"./{fileName[0]}.pdf")
